<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <meta content="initial-scale=1.0, user-scalable=no, width=device-width" name="viewport">
    <title>下属行政区查询</title>
</head>
<body>
<script src="https://cdn.bootcss.com/jquery/2.1.1-rc2/jquery.js"
        type="text/javascript"></script>
<script src="https://webapi.amap.com/maps?v=1.4.15&key=d5c3d02965103b4abb1aaed7588b9958&plugin=AMap.DistrictSearch"
        type="text/javascript"></script>
<script type="text/javascript">
    var district, polygons = [];
    var totalData = [];
    var level = ["country", 'province', 'city', 'district', 'street'];
    //行政区划查询
    var opts = {
        // adcode: "中国", //440000
        subdistrict: 1,   //返回下一级行政区
        showbiz: false,  //最后一级返回街道信息
        extensions: 'all',
    };
    var temp = undefined;
    district = new AMap.DistrictSearch(opts);//注意：需要使用插件同步下发功能才能这样直接使用

    var counter = 0;

    function getData(opts) {
        counter++;
        district.search(opts.adcode || '中国', $.proxy(function (c, status, result) {
            if (status === 'complete') {
                doGgetData(result.districtList[0], c);
            }
            counter--;
        }, this, opts));
    }

    function doGgetData(data, c) {
        var bounds = data.boundaries;
        let geoJson = undefined;
        if (data.level !== 'country' && bounds) {
            var cooo = [];
            for (var i = 0, l = bounds.length; i < l; i++) {
                var out = bounds[i];
                var coo = [];
                for (var j = 0; j < out.length; j++) {
                    coo.push([out[j].lng, out[j].lat]);
                }
                coo = [coo];
                cooo.push(coo);
            }
            var type = 'Polygon';
            if (cooo.length === 1) {
                type = 'Polygon';
                cooo = cooo[0];
            } else {
                type = 'MultiPolygon';
            }
            geoJson = JSON.stringify({
                "type": type,
                "coordinates": cooo
            });
        }
        var obj = {
            areaName: data.name,
            center: JSON.stringify([data.center.lng, data.center.lat]),
            geoJson: geoJson,
            areaType: type,
            code: data.adcode,
            parentCode: c.pcode || "-1",
            level: data.level.toUpperCase()
        };

        totalData.push(obj);
        var subList = data.districtList;
        if (subList && subList.length > 0) {//
            if (subList[0].level === "street") {
                var arr = [];
                for (var i = 0, l = subList.length; i < l; i++) {
                    var index = i;
                    var sub = subList[i];
                    var ss = index + "";
                    if (ss.length === 1) {
                        ss = "0" + ss;
                    }
                    var obj = {
                        areaName: sub.name,
                        center: JSON.stringify([sub.center.lng, sub.center.lat]),
                        areaType: type,
                        code: sub.adcode + ss,
                        parentCode: data.adcode || "-1",
                        level: sub.level.toUpperCase()
                    };
                    totalData.push(obj);
                }
            } else {
                for (var i = 0, l = subList.length; i < l; i++) {
                    (function (sub, index) {
                        if (sub.level === "country"
                            || sub.level === "province"
                            || sub.level === "city"
                            || sub.level === "district") {

                            var newOpts = {
                                pcode: data.adcode,
                                adcode: sub.adcode,
                                subdistrict: 1,   //返回下一级行政区
                                showbiz: false,  //最后一级返回街道信息
                                extensions: 'all',
                            };

                            counter++;
                            setTimeout(function () {
                                counter--;
                                this.getData(newOpts);
                            }, Math.random() * 18000);
                        } else if (sub.level === "street") {

                        }
                    })(subList[i], i);
                }
            }

        }
    }

    getData(opts);

    var oooo = setInterval(function () {
        if (counter == 0) {
            clearInterval(oooo);
            console.log(JSON.stringify(totalData));
            if (confirm("是否保存数据！")) {
                $.ajax({
                    type: 'post',
                    url: "http://localhost:7082/sys/region/commitList",
                    dataType: 'json',
                    data: JSON.stringify(totalData),
                    xhrFields: {withCredentials: true},
                    contentType: 'application/json',
                    success: function (result) {
                        if (result.success) {
                            console.log(sub.name + " : " + sub.adcode)
                            // console.log(JSON.stringify(obj));
                            // alert("保存成功！");
                        }
                    }
                });
            }
        }
    }, 1000);

</script>

</body>
</html>
